package sample.test.cc.cool.gui.gca;

import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import sample.test.cc.cool.gui.controllers.GCAController;

public class GuiGCA extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JButton startButton = null;
	private JLabel ipGCA = null;
	private JList transactionsList = null;
	private JLabel selectedTransactionLabel = null;
	private JLabel selectedTrasactionStatusLabel = null;
	private JScrollPane jScrollPane = null;
	private JTable participantsTable = null;
	private JLabel gcaPort = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextPane loggerPane = null;
	private JLabel loggerLabel = null;
	private JScrollPane jScrollPane1 = null;
	private JSeparator panelSeparator12 = null;
	private JLabel jLabel2 = null;
	private JSeparator panelSeparator121 = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel4 = null;
	/**
	 * This is the default constructor
	 */
	public GuiGCA() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(648, 516);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		this.setTitle("eBESA - GCA - Monitor");
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(440, 100, 71, 21));
			jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel4.setText("Status:");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(150, 100, 81, 21));
			jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
			jLabel3.setText("Transaction:");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(10, 90, 101, 21));
			jLabel2.setText("Transactions");
			loggerLabel = new JLabel();
			loggerLabel.setBounds(new Rectangle(10, 340, 51, 21));
			loggerLabel.setText("Logger");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(180, 15, 46, 31));
			jLabel1.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel1.setFont(new Font("Dialog", Font.BOLD, 14));
			jLabel1.setText("PORT:");
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(15, 15, 31, 31));
			jLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
			jLabel.setFont(new Font("Dialog", Font.BOLD, 14));
			jLabel.setText("IP:");
			gcaPort = new JLabel();
			gcaPort.setBounds(new Rectangle(240, 15, 46, 31));
			gcaPort.setFont(new Font("Dialog", Font.BOLD, 12));
			gcaPort.setHorizontalTextPosition(SwingConstants.CENTER);
			gcaPort.setHorizontalAlignment(SwingConstants.CENTER);
			gcaPort.setText("");
			selectedTrasactionStatusLabel = new JLabel();
			selectedTrasactionStatusLabel.setBounds(new Rectangle(520, 100, 101, 19));
			selectedTrasactionStatusLabel.setHorizontalAlignment(SwingConstants.CENTER);
			selectedTrasactionStatusLabel.setText("-");
			selectedTransactionLabel = new JLabel();
			selectedTransactionLabel.setBounds(new Rectangle(240, 100, 81, 20));
			selectedTransactionLabel.setHorizontalAlignment(SwingConstants.CENTER);
			selectedTransactionLabel.setText("-");
			ipGCA = new JLabel();
			ipGCA.setBounds(new Rectangle(60, 15, 106, 31));
			ipGCA.setHorizontalTextPosition(SwingConstants.CENTER);
			ipGCA.setFont(new Font("Dialog", Font.BOLD, 12));
			ipGCA.setToolTipText("The IP address of the GCA");
			ipGCA.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			ipGCA.setHorizontalAlignment(SwingConstants.CENTER);
			ipGCA.setText("");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getStartButton(), null);
			jContentPane.add(ipGCA, null);
			jContentPane.add(selectedTransactionLabel, null);
			jContentPane.add(selectedTrasactionStatusLabel, null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(gcaPort, null);
			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getLoggerPane(), null);
			jContentPane.add(loggerLabel, null);
			jContentPane.add(getJScrollPane1(), null);
			jContentPane.add(getPanelSeparator12(), null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getPanelSeparator121(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(jLabel4, null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes StartButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getStartButton() {
		if (startButton == null) {
			startButton = new JButton();
			startButton.setBounds(new Rectangle(525, 15, 93, 31));
			startButton.setText("Start");
			startButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					GCAController.getInstance().StartGCA();
					startButton.setEnabled(false);
				}
			});
		}
		return startButton;
	}

	/**
	 * This method initializes transactionsList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getTransactionsList() {
		if (transactionsList == null) {
			transactionsList = new JList();
			transactionsList.setModel(new DefaultListModel());
			transactionsList.addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent arg0) {
					
					long id = Long.parseLong((String)transactionsList.getSelectedValue());
					GCAController.getInstance().setSelectedTransaction(id);
					GCAController.getInstance().getTransactionData(id);
				}
			});
			
		}
		return transactionsList;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(150, 135, 481, 176));
			jScrollPane.setViewportView(getParticipantsTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes participantsTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getParticipantsTable() {
		if (participantsTable == null) {
			participantsTable = new JTable();
			participantsTable.setRowSelectionAllowed(false);
			DefaultTableModel df = new DefaultTableModel();
			df.addColumn("BESA Conatinner");
			df.addColumn("GCC Location");
			df.addColumn("Vote");
			participantsTable.setModel(df);
		}
		return participantsTable;
	}

	/*
	 * THIS METHOD ALLOW THE INTERACTION BETWEEN THE VIEW AND THE CONTROLLER
	 */
	
	public void setGCAData(String location, String port){
		this.ipGCA.setText(location);
		this.gcaPort.setText(port);
	}
	
	public void appendToLogger(String text){
		getLoggerPane().setText(getLoggerPane().getText()+text);
	}
	
	public void addTransactionList(String newTransaction){
		DefaultListModel model = (DefaultListModel) this.getTransactionsList().getModel();
		model.addElement(newTransaction);
		this.getTransactionsList().repaint();
	}
	
	//---------------------------------------------------------------------

	/**
	 * This method initializes loggerPane	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JTextPane getLoggerPane() {
		if (loggerPane == null) {
			loggerPane = new JTextPane();
			loggerPane.setBounds(new Rectangle(15, 375, 616, 91));
			loggerPane.setContentType("text/html");
			loggerPane.setEditable(false);
		}
		return loggerPane;
	}
	
	public void updateTransactionData(String id, String status, DefaultTableModel model){
		this.selectedTransactionLabel.setText(id);
		this.selectedTrasactionStatusLabel.setText(status);
		this.updateTable(model);
	}
	
	public void updateStatus(String status){
		this.selectedTrasactionStatusLabel.setText(status);
	}

	/**
	 * This method initializes jScrollPane1	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane1() {
		if (jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(new Rectangle(10, 135, 96, 176));
			jScrollPane1.setViewportView(getTransactionsList());
		}
		return jScrollPane1;
	}

	/**
	 * This method initializes panelSeparator12	
	 * 	
	 * @return javax.swing.JSeparator	
	 */
	private JSeparator getPanelSeparator12() {
		if (panelSeparator12 == null) {
			panelSeparator12 = new JSeparator(JSeparator.VERTICAL);
			panelSeparator12.setBounds(new Rectangle(120, 90, 16, 226));
		}
		return panelSeparator12;
	}

	/**
	 * This method initializes panelSeparator121	
	 * 	
	 * @return javax.swing.JSeparator	
	 */
	private JSeparator getPanelSeparator121() {
		if (panelSeparator121 == null) {
			panelSeparator121 = new JSeparator(JSeparator.HORIZONTAL);
			panelSeparator121.setBounds(new Rectangle(60, 350, 571, 11));
		}
		return panelSeparator121;
	}

	public void updateTable(DefaultTableModel model) {
		getParticipantsTable().setModel(model);
		
	}
	
	
}  //  @jve:decl-index=0:visual-constraint="34,13"
